/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.spring.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import team.mai.antexpress.dao.PermissionGroupDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.PermissionGroupEntity;
import team.mai.antexpress.services.PermissionGroupService;
import team.mai.antexpress.services.UserService;
import team.mai.antexpress.spring.beans.LoginBean;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static team.mai.antexpress.services.PermissionGroupService.INSUFFICIENT_PRIVILEGE;
import static team.mai.antexpress.services.PermissionGroupService.INTERNAL_ERROR;

@Controller
public class PermGroupController {

	@Autowired
	LoginBean login;
	@Autowired
	PermissionGroupService permissionGroupService;
	@Autowired
	PermissionGroupDAO permissionGroupDAO;
	@Autowired
	UserService userService;
	@Autowired
	UserDAO userDAO;

	@RequestMapping("/admin/permission_groups")
	public String doGet(ModelMap map) {
		List<PermissionGroupEntity> permissionGroups = permissionGroupService.getPermissionGroups(login.getUsername());
		if (permissionGroups != null)
			map.addAttribute("groups", permissionGroups);
		map.addAttribute("permissions", userDAO.getPermissionByUser(login.getUsername()));
		return "admin/manage_permission_groups";
	}

	@RequestMapping(value = "/admin/delete_permission_group", method = RequestMethod.POST)
	@ResponseBody
	public String deletePermissionGroups(HttpServletRequest request) {
		String[] ids = request.getParameterValues("ids[]");
		if (ids == null)
			return null;
		ArrayList<Integer> idList = new ArrayList<>();
		for (String id : ids) {
			idList.add(Integer.parseInt(id));
		}
		boolean b = permissionGroupService.deletePermissionGroups(login.getUsername(), idList);
		if (b) {
			return "{\"status\":\"ok\"}";
		} else {
			return "{\"status\":\"fail\"}";
		}
	}

	@RequestMapping(value = "/admin/modify_permission_group", method = RequestMethod.GET)
	public String modifyPermissionGroupGet(int groupId, ModelMap map) {
		PermissionGroupEntity group = permissionGroupService.getPermissionGroup(login.getUsername(), groupId);
		if (group == null) {
			map.addAttribute("permissionGroup", group);
			map.addAttribute("errorMessage",
					"Failed to fetch group #" + groupId + ".");
		} else {
			map.addAttribute("permissionGroup", group);
		}
		return "admin/modify_permission_group";
	}

	@RequestMapping(value = "/admin/modify_permission_group", method = RequestMethod.POST)
	@ResponseBody
	public String modifyPermissionGroupPost(PermissionGroupEntity group) {
		if (permissionGroupService.updatePermissionGroup(login.getUsername(), group)) {
			return "{\"status\":\"success\"}";
		} else {
			return "{\"status\":\"fail\",\"errorMessage\":\"Internal Error or Insufficient Privilege\"}";
		}
	}

	@RequestMapping(value = "/admin/ajax/check_permission_group_name", method = RequestMethod.POST)
	@ResponseBody
	public String checkPermissionGroupName(String except, String groupName) {
		if (permissionGroupDAO.ifExistsPermissionGroup(except, groupName)) {
			return "{\"exists\":true}";
		} else {
			return "{\"exists\":false}";
		}
	}

	@RequestMapping(value = "/admin/new_permission_group" ,method = RequestMethod.GET)
	public String doGetNewPermGroup(){
		return "admin/new_permission_group";
	}

	@RequestMapping(value = "/admin/new_permission_group", method = RequestMethod.POST)
	@ResponseBody
	public String insertPermissionGroup(PermissionGroupEntity group){
		group.setId(0);
		int id = permissionGroupService.insertPermissionGroup(login.getUsername(), group);
		if(id > 0){
			return "{\"status\":\"success\",\"groupId\":" + id +" }";
		} else {
			String errorMessage;
			switch (id){
				case INTERNAL_ERROR: errorMessage = "Internal Error";break;
				case INSUFFICIENT_PRIVILEGE: errorMessage = "Insufficient Privilege";break;
				default: errorMessage = "Unknown Error";
			}
			return "{\"status\":\"failed\",\"errorMessage\":\"" + errorMessage + "\"}";
		}
	}
}
